package com.naiterui.ehp.bs.cms.modules.oms.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.advice.IgnoreResponseEntityWrap;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.utils.FileUtil;
import com.naiterui.ehp.bs.cms.common.controller.AbstractController;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.oms.feign.IOrderFeignClient;
import com.naiterui.ehp.bs.cms.modules.oms.service.IOrderService;
import com.naiterui.ehp.bs.cms.modules.oms.vo.OrderVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.StatisticalVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.UpdateOrderVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 订单管理
 */
@Slf4j
@Validated
@Api(tags = {SwaggerTag.CMS + SwaggerTag.ORDER})
@RestController
@AllArgsConstructor
@RequestMapping("oms/order")
public class OrderController extends AbstractController {

  private final IOrderService orderService;
  private final IOrderFeignClient orderFeignClient;

  /**
   * 列表
   *
   * @return
   */
  @SysLog("订单列表")
  @GetMapping("/list")
  @ApiOperation("订单列表")
  @PreAuthorize("@p.check('oms:order:list')")
  public PageVO<OrderVO> list(PageParamsVO pageParamsVO, OrderVO order) {
    return this.orderService.queryPage(pageParamsVO, order);
  }

  @SysLog("订单详情")
  @GetMapping("/details/{orderId}")
  @ApiOperation("订单详情")
  @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "Long", paramType = "path", required = true)
  @PreAuthorize("@p.check('oms:order:list')")
  public OrderVO details(@PathVariable("orderId") Long orderId) {
    return this.orderService.getOrderDetails(orderId);
  }

  @SysLog("订单审核")
  @PostMapping("/audit")
  @ApiOperation("订单审核")
  @PreAuthorize("@p.check('oms:order:audit')")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "orderIds", value = "订单ID数组", dataType = "Long", paramType = "body", required = true),
  })
  public void audit(@RequestBody Set<Long> orderIds) throws BusinessException {
    log.info("订单审核 orderIds:{} ", orderIds);
    this.orderFeignClient.audit(orderIds);
  }

  @SysLog("订单反审核")
  @PostMapping("/return/audit")
  @ApiOperation("订单反审核")
  @PreAuthorize("@p.check('oms:order:returnAudit')")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "orderIds", value = "订单ID数组", dataType = "Long", paramType = "body", required = true),
  })
  public void returnAudit(@RequestBody Set<Long> orderIds) throws BusinessException {
    log.info("订单反审核 orderIds:{} ", orderIds);
    this.orderFeignClient.returnAudit(orderIds);
  }

  @SysLog("取消订单")
  @ApiOperation("取消订单")
  @PreAuthorize("@p.check('oms:order:cancel')")
  @PostMapping("/cancel")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "orderIds", value = "订单ID数组", dataType = "Long", paramType = "body", required = true),
  })
  public void cancel(@RequestBody Set<Long> orderIds) throws BusinessException {
    log.info("取消订单 orderIds:{} ", orderIds);
    this.orderFeignClient.cancel(orderIds);
  }

  @SysLog("修改订单")
  @ApiOperation("修改订单")
  @PreAuthorize("@p.check('oms:order:update')")
  @PostMapping
  public void update(@RequestBody UpdateOrderVO updateOrderVO) throws BusinessException {
    log.info("修改订单 updateOrderVO:{} ", updateOrderVO);
    this.orderFeignClient.update(updateOrderVO);
  }

  /**
   * 商品信息
   *
   * @return
   */
  @SysLog("获取统计数据")
  @ApiOperation("获取统计数据")
  @GetMapping("/statistical")
  @PreAuthorize("@p.check('oms:order:statistical')")
  public List<StatisticalVO> statistical(PageParamsVO pageParamsVO, OrderVO order) {
    log.info("获取统计数据 请求参数：order = {}", order);
    return this.orderService.getStatisticalVO(pageParamsVO, order);
  }

  @GetMapping("export")
  @ApiOperation("导出订单")
  @IgnoreResponseEntityWrap
  @PreAuthorize("@p.check('oms:order:orderExport')")
  public void exportOrder(PageParamsVO pageParamsVO, OrderVO order, HttpServletResponse response) throws IOException {
    byte[] body = this.orderService.export(pageParamsVO, order);
    String fileName = System.getProperty("java.io.tmpdir") + File.separator + System.currentTimeMillis() + ".xls";
    FileUtil.downloadFile(this.request, response, FileUtil.writeBytes(body, fileName), true);
  }

}
